using System.Collections.Generic;
using System.IO;
using System.Text;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base;

namespace FireStarter.CodeGeneration
{
    public class SolutionGenerator : ISolutionGenerator
    {
        public SolutionGenerator()
        {
            ProjectGenerators = new List<IProjectGenerator>();
            ModelClasses = new List<IModelClass>();
        }

        public string SolutionName { get; set; }
        public string SolutionDirectory { get; set; }
        public List<IProjectGenerator> ProjectGenerators { get; private set; }
        public string HabaneroReferencePath { get; set; }
        public string SolutionTypeName { get; set; }
        public IModelSolution ModelSolution { get; set; }
        public IList<IModelClass> ModelClasses { get; set; }
        public string ClassDefsString { get; set; }
        public CodeTemplateEnum CodeTemplate { get; set; }

        public string SolutionFileName
        {
            get { return Path.Combine(SolutionDirectory, SolutionName + ".sln"); }
        }

        public Result Generate(CodeTemplateEnum codeTemplate)
        {
            if (SolutionName == null) throw new PropertyMissingException("SolutionName");
            if (SolutionDirectory == null) throw new PropertyMissingException("SolutionDirectory");
            SolutionFileManager solutionFileManager = new SolutionFileManager(SolutionFileName, codeTemplate);

            List<string> projectFileNames = GetProjectFileNames();
            projectFileNames.Sort(); // First one in list becomes the startup project

            foreach (string projectFileName in projectFileNames)
            {
                solutionFileManager.AddProjectFile(projectFileName);
            }
            solutionFileManager.SaveSolutionFile();
            return new Result(true);
        }

        private List<string> GetProjectFileNames()
        {
            List<string> projectFileNames = new List<string>();
            foreach (IProjectGenerator projectGenerator in ProjectGenerators)
            {
                projectFileNames.Add(projectGenerator.ProjectFileName);
            }
            return projectFileNames;
        }
    }
}
